package com.clothing;

import javax.persistence.Entity;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName: GenRps
 * @Description: 生成组件
 * @author: Charles
 * @date: 2018/5/17 19:17
 * @version: 1.0
 */
public class GenRps {
    private static final String srcPath = "/src/main/java/";
    private static final String author = "Charles";
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    private static final String templatePath = "D:/idea_workspace/template/";

    private enum Type {
        Dao("com.clothing.dao"),
        DaoImpl("com.clothing.dao.impl"),
        Service("com.clothing.service"),
        ServiceImpl("com.clothing.service.impl");
        private String code;

        Type(String code) {
            this.setCode(code);
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }
    }

    public static void main(String[] args) {
        for (Type type : Type.values()) {
            gen("com.clothing.entity", type);
        }
    }

    private static void gen(String entityPackage, Type type) {
        String dirPath = "/" + entityPackage.replace('.', '/');
        File dir = new File(GenRps.class.getResource(dirPath).getFile());
        for (File f : dir.listFiles()) {
            if (!f.isFile()) {
                continue;
            }
            String fn = f.getName();
            if (!fn.endsWith(".class")) {
                continue;
            }
            try {
                Class<?> c = Class.forName(entityPackage + "." + fn.substring(0, fn.length() - 6));
                Entity e = c.getAnnotation(Entity.class);
                if (e == null) {
                    continue;
                }
                gen(type, c, Long.class);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    private static void gen(Type type, Class<?> entityClass, Class<?> idClass) {
        String idcStr;
        if (idClass.getPackage().getName().equals("java.lang")) {
            idcStr = idClass.getSimpleName();
        } else {
            idcStr = idClass.getName();
        }
        gen(type, entityClass.getPackage().getName(), entityClass.getSimpleName(), idcStr);
    }

    private static void gen(Type type, String entityPackage, String entityName, String idClass) {
        InputStream in = getReader(type);
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            gen(type, entityPackage, entityName, idClass, reader);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static InputStream getReader(Type type) {
        // String fn = "/" + GenRps.class.getPackage().getName().replace('.',
        // '/') + "/" + type.toString() + ".tpl";
        // InputStream in = GenRps.class.getResourceAsStream(fn);
        InputStream in = null;
        try {
            in = new FileInputStream(templatePath + type.toString() + ".tpl");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return in;
    }

    private static void gen(Type type, String entityPackage, String entityName, String idClass, BufferedReader reader) {
        File f = new File(getFilePath(type.getCode()) + entityName + type.toString() + ".java");
        FileWriter fw = null;
        try {
            if (f.exists()) {
                return;
            }
            System.out.println(f.getPath());
            fw = new FileWriter(f);
            String date = sdf.format(new Date());
            String line;
            while ((line = reader.readLine()) != null) {
                fw.write(replace(type, line, entityPackage, entityName, idClass,
                        date));
                fw.write("\r\n");
                fw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static String replace(Type type, String src, String entityPackage, String entityName, String idClass, String date) {
        src = src.replace("${thisPackage}", type.getCode());
        src = src.replace("${entityPackage}", entityPackage);
        src = src.replace("${entityName}", entityName);
        src = src.replace("${idClass}", idClass);
        src = src.replace("${author}", author);
        src = src.replace("${date}", date);
        return src;
    }

    private static String getFilePath(String thisPackage) {
        return System.getProperty("user.dir") + srcPath + thisPackage.replace('.', '/') + "/";
    }
}
